class HorsesController < ApplicationController

  def live_search
    @search = params[:searching]
    @results = Horseslist.search(params[:searching], params[:page])
    @number_match = @results.length
    render(:layout => false)
  end
  
  def list
    @horses = horse.find_all    
  end
  def prove
    @provenances = provenance.find_all
  end
  # GET /horses
  # GET /horses.xml
  def index
    @horses = Horseslist.search(params[:search],params[:page])
    @search = params[:search]
    @user = User.new
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @horses }
    end
  end
  
  # GET /horses/1
  # GET /horses/1.xml
  def show
    @horse = Horse.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @horse }
    end
  end
  
  # GET /horses/new
  # GET /horses/new.xml
  def new
    if authorize
      @horse = Horse.new
      @horsesexes = Horsesexe.find(:all)
      @families = Familie.find(:all)
      
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @horse }
      end
    else 
      if session[:user_id]
        redirect_to(:controller => 'pendinghorses', :action => 'new')
      end
   end
  end
  
  # GET /horses/1/edit
  def edit
    if authorize
      @horse = Horse.find_by_id(params[:id])
      @horsesexes = Horsesexe.find(:all)
      @families = Familie.find(:all)
      @provenances = Provenance.find(:all)
      @horsedata = Horsedata.find_by_horse_id(@horse.id)
      unless @horsedata
        @horsedata = Horsedata.new
      end
    else
      flash[:notice] = "<div class='invisible'>Solo Administradores<div>"
      redirect_to :controller => 'public'
    end
  end
  
  def find_fam
    @families = Familie.find(:all, :conditions => ["provenance_id = ?", params[:value]]).map {|u| [u.family_name, u.id]}
  end
  # POST /horses
  # POST /horses.xml
  def create
    @horse = Horse.new(params[:horse])
    
    respond_to do |format|
      if @horse.save
        flash[:notice] = "<div class='invisible'>Horse was successfully created.</div>"
        format.html { redirect_to(@horse) }
        format.xml  { render :xml => @horse, :status => :created, :location => @horse }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @horse.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def create2
    @horsedata = Horsedata.new(params[:horsedata])
    
    respond_to do |format|
      if @horsedata.save
        flash[:notice] = "<div class='invisible'>Horsedata was successfully created.</div>"
        format.html { redirect_to(:controller => 'horses', :action => 'edit', :id => @horsedata.horse_id) }
        #        format.xml  { render :xml => @horsedata, :status => :created, :location => @horsedata }
      else
      end
    end
  end
  
  # PUT /horses/1
  # PUT /horses/1.xml
  def update
    @horse = Horse.find(params[:id])
    
    respond_to do |format|
      if @horse.update_attributes(params[:horse])
        flash[:notice] = "<div class='invisible'>Horse was successfully updated.</div>"
        format.html { redirect_to(@horse) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @horse.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def update2
    @horsedata = Horsedata.find(params[:id])
    respond_to do |format|
      if @horsedata.update_attributes(params[:horsedata])
        flash[:notice] = "<div class='invisible'>Horsedata was successfully updated<div>"
        format.html { redirect_to(:controller => 'horses', :action => 'edit', :id => @horsedata.horse_id) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @horsedata.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /horses/1
  # DELETE /horses/1.xml
  def destroy
    if authorize
      @horse = Horse.find(params[:id])
      @horse.destroy
      
      respond_to do |format|
        format.html { redirect_to(horses_url) }
        format.xml  { head :ok }
      end
    end
  end
  protected
  def authorize
    unless session[:user_id]
      flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
      redirect_to :controller => 'admin', :action => 'login', :class => 'nyroModal', :rev=>'gal'
    else
      user = User.find(session[:user_id])
      if user.user_tipe_id == 1
        return true        
      else
        return false
      end
    end
  end
end
